package com.zhuoyue.biz.core.command.cmd.common.infrared;

import com.zhuoyue.biz.core.api.domain.InfraredDeviceControlDO;
import com.zhuoyue.biz.core.api.dto.req.ControlReq;
import com.zhuoyue.biz.core.command.constant.InfraredCmdDeal;
import com.zhuoyue.common.constant.ControlType;
import com.zhuoyue.common.utils.CmdUtils;
import com.zhuoyue.common.utils.DataTypeUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;


/**
 * 红外控制
 *
 * @author wjming
 */
public class Infrared {
    public static String control(InfraredDeviceControlDO infrared, ControlReq controlReq) {
        String cmd = "";
        switch (controlReq.getControlType()) {
            //空调开关命令发送
            case ControlType.TURN_ON_REQUEST:
                cmd = turnOnOffAc(infrared, "FF");
                break;
            case ControlType.TURN_OFF_REQUEST:
                cmd = turnOnOffAc(infrared, "00");
                break;
            //设置温度
            case ControlType.SET_TEMPERATURE_REQUEST:
                cmd = getAcTempSendCmd(infrared, controlReq);
                break;
            case ControlType.SET_MODE_REQUEST:
                cmd = getAcModeSendCmd(infrared, controlReq);
                break;
            case ControlType.SET_FAN_SPEED_REQUEST:
                cmd = getAcWindSpeedSendCmd(infrared, controlReq);
                break;
            default:
                break;
        }
        return cmd;
    }

    private static String getAcWindSpeedSendCmd(InfraredDeviceControlDO infrared, ControlReq controlReq) {

        Map<String, Object> actionsMap = controlReq.getActions();
        String windSpeed = actionsMap.get("windSpeed").toString();

        String value = "";
        switch (windSpeed) {
            //自动
            case ControlType.SetWindSpeed.AUTO:
                value = "00";
                break;
            //低风速
            case ControlType.SetWindSpeed.LOW:
                value = "01";
                break;
            //中风速
            case ControlType.SetWindSpeed.MID:
                value = "02";
                break;
            //高风速
            case ControlType.SetWindSpeed.HIGH:
                value = "03";
                break;
            default:
                value = "00";
                break;

        }

        String acWindSpeedCmd1To4 = InfraredCmdDeal.AcWindSpeedSend.CMD_1 +
                value +
                InfraredCmdDeal.AcWindSpeedSend.CMD_3 +
                InfraredCmdDeal.AcWindSpeedSend.CMD_4;

        String acWindSpeedCmd5 = DataTypeUtil.getAddCheckByXor(acWindSpeedCmd1To4);

        String acWindSpeedCmd = acWindSpeedCmd1To4 + acWindSpeedCmd5;

        String fullCmd = getCommonCmd(acWindSpeedCmd, infrared.getGatewayMac(), infrared.getDeviceMac());

        return fullCmd;
    }

    private static String getAcModeSendCmd(InfraredDeviceControlDO infrared, ControlReq controlReq) {
        Map<String, Object> actionsMap = controlReq.getActions();
        String mode = actionsMap.get("mode").toString();

        String value = "";
        switch (mode) {
            //自动
            case ControlType.SetMode.AUTO:
                value = "00";
                break;
            //制冷
            case ControlType.SetMode.COOL:
                //制冷
            case ControlType.SetMode.COLD:
                value = "01";
                break;
            //除湿
            case ControlType.SetMode.DEHUMIDIFICATION:
                value = "02";
                break;
            //送风
            case ControlType.SetMode.WIND:
                //送风
            case ControlType.SetMode.FAN:
                value = "03";
                break;
            //制热
            case ControlType.SetMode.HEAT:
                value = "04";
                break;
            default://默认自动
                value = "00";
                break;
        }

        String acModeSendCmd1To4 = InfraredCmdDeal.AcModeSend.CMD_1 +
                value +
                InfraredCmdDeal.AcModeSend.CMD_3 +
                InfraredCmdDeal.AcModeSend.CMD_4;

        String acModeSendCmd5 = DataTypeUtil.getAddCheckByXor(acModeSendCmd1To4);

        String acModeSendCmd = acModeSendCmd1To4 + acModeSendCmd5;

        String fullCmd = getCommonCmd(acModeSendCmd, infrared.getGatewayMac(), infrared.getDeviceMac());

        return fullCmd;
    }

    /**
     * 温度控制
     *
     * @param infrared
     * @param controlReq
     * @return
     */
    private static String getAcTempSendCmd(InfraredDeviceControlDO infrared, ControlReq controlReq) {
        Map<String, Object> actionsMap = controlReq.getActions();
        Integer temp = (Integer) actionsMap.get("temperature");

        String acTempSendCmd1To4 = "06" + CmdUtils.intToHexTwo(temp) + "08" + "08";

        String acTempSendCmd5 = DataTypeUtil.getAddCheckByXor(acTempSendCmd1To4);

        String acTempSendCmd = acTempSendCmd1To4 + acTempSendCmd5;

        String fullCmd = getCommonCmd(acTempSendCmd, infrared.getGatewayMac(), infrared.getDeviceMac());

        return fullCmd;
    }

    private static String turnOnOffAc(InfraredDeviceControlDO infrared, String onOrOff) {
        String acSwitchSendCmd1To4 = "04" + onOrOff + "08" + "08";

        String acSwitchSendCmd5 = DataTypeUtil.getAddCheckByXor(acSwitchSendCmd1To4);

        String acSwitchSendCmd = acSwitchSendCmd1To4 + acSwitchSendCmd5;

        String fullCmd = getCommonCmd(acSwitchSendCmd, infrared.getGatewayMac(), infrared.getDeviceMac());

        return fullCmd;
    }

    private static String getCommonCmd(String infraredCmd, String gatewayMac, String deviceMac) {

        String infraredCommonCmd7 = CmdUtils.getDateLength(infraredCmd);

        String infraredCommonCmd3To8 = InfraredCmdDeal.InfraredCommon.CMD_3 +
                InfraredCmdDeal.InfraredCommon.CMD_4 +
                gatewayMac +
                deviceMac +
                infraredCommonCmd7 +
                infraredCmd;

        String infraredCommonCmd2 = CmdUtils.getDateLength(infraredCommonCmd3To8);

        String infraredCommonCmd9 = CmdUtils.getCmdBodySum(infraredCommonCmd3To8);

        String fullCmd = InfraredCmdDeal.InfraredCommon.CMD_1 +
                infraredCommonCmd2 +
                infraredCommonCmd3To8 +
                infraredCommonCmd9 +
                InfraredCmdDeal.InfraredCommon.CMD_10;

        return StringUtils.upperCase(fullCmd);

    }
}
